package com.youlv.yl.service.impl;

import com.youlv.yl.dto.CommentDto;
import com.youlv.yl.dto.CommentReplyDto;
import com.youlv.yl.entity.mood.MoodComment;
import com.youlv.yl.dao.MoodCommentDao;
import com.youlv.yl.service.intf.MoodCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlv.yl.util.StringUtils;
import com.youlv.yl.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fenghe
 * @since 2022-01-17
 */
@Service
public class MoodCommentServiceImpl extends ServiceImpl<MoodCommentDao, MoodComment> implements MoodCommentService {
    @Autowired
    private MoodCommentDao dao;

    @Override
    public R save(CommentDto dto) {
        // 入参验证
        if(dto != null && StringUtils.isNotEmpty(dto.getContent())) {
            // 创建对象，存储数据库
            MoodComment comment = new MoodComment();
            comment.setContent(dto.getContent());
            comment.setCtime(new Date());
            comment.setType(1);
            comment.setParentid(0L);
            comment.setUmid(dto.getUmid());
            comment.setUid(dto.getUid());
            dao.insert(comment);
            return R.ok();
        }
        return R.fail("请检查参数信息");
    }

    @Override
    public R saveRep(CommentReplyDto dto) {
        // 入参验证
        if(dto != null && StringUtils.isNotEmpty(dto.getContent()) && dto.getParentid() > 0) {
            // 创建对象，存储数据库
            MoodComment comment = new MoodComment();
            comment.setContent(dto.getContent());
            comment.setUid(dto.getUid());
            comment.setParentid(dto.getParentid());
            comment.setUmid(dto.getUmid());
            comment.setCtime(new Date());
            comment.setType(2);
            dao.insert(comment);
            return R.ok();
        }
        return R.fail("请检查参数信息");
    }

    @Override
    public R queryByMid(Long mid) {
        // 验证
        if(mid > 0) {
            // 查询，
            return R.ok(dao.selectMid(mid));
        }
        return R.fail("请检查参数信息");
    }
}
